package com.haiyi.software.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.haiyi.software.app.basic.RegisterSexActivityBasic;
import com.haiyi.software.entity.BeanRegResult;
import com.haiyi.software.entity.UserInfo;

@SuppressLint("ResourceAsColor")
public class RegisterSex extends RegisterSexActivityBasic implements
		OnClickListener {

	// 完成按钮
	Button finishBtn;

	CheckBox ck_sex_male, ck_sex_female, ck_sex_1, ck_sex_0, ck_sex_05,
			ck_sex_t, ck_sex_p, ck_sex_h, ck_sex_tc, ck_sex_bi;

	CheckBox boxs[] = new CheckBox[10];

	String str_uname, str_uid, str_pwd, str_uguid;

	EditText Editx_username;

	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_sex);
		sp = this.getSharedPreferences("WHOESYOU", Context.MODE_PRIVATE);
		initParam();

		initComp();

		finishBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (Editx_username.getText().toString().trim().length() > 0
						&& Editx_username.getText().toString().trim().length() <= 20) {

					watingDialog.show();

					regSex(str_uid, str_uname, str_pwd, str_uguid,
							getCheckedSex(), Editx_username.getText()
									.toString().trim());
				} else {
					Toast.makeText(RegisterSex.this, "昵称长度应为1至20个字符之时",
							Toast.LENGTH_LONG).show();
				}

			}

		});
	}

	private void initParam() {
		// TODO 自动生成的方法存根
		str_uid = getIntent().getStringExtra("uid");
		str_pwd = getIntent().getStringExtra("pwd");
		str_uname = getIntent().getStringExtra("uname");
		str_uguid = getIntent().getStringExtra("uguid");
	}

	public void initComp() {

		Editx_username = (EditText) findViewById(R.id.Editx_username);
		finishBtn = (Button) findViewById(R.id.nextBtn_sex);
		ck_sex_0 = (CheckBox) this.findViewById(R.id.ck_sex_0);
		ck_sex_05 = (CheckBox) this.findViewById(R.id.ck_sex_05);
		ck_sex_1 = (CheckBox) this.findViewById(R.id.ck_sex_1);
		ck_sex_female = (CheckBox) this.findViewById(R.id.ck_sex_female);
		ck_sex_h = (CheckBox) this.findViewById(R.id.ck_sex_h);
		ck_sex_male = (CheckBox) this.findViewById(R.id.ck_sex_male);
		ck_sex_p = (CheckBox) this.findViewById(R.id.ck_sex_p);
		ck_sex_t = (CheckBox) this.findViewById(R.id.ck_sex_t);
		ck_sex_bi = (CheckBox) this.findViewById(R.id.ck_sex_bi);
		ck_sex_tc = (CheckBox) this.findViewById(R.id.ck_sex_tc);

		ck_sex_male.setTag("1");
		ck_sex_female.setTag("2");
		ck_sex_1.setTag("3");
		ck_sex_0.setTag("4");
		ck_sex_05.setTag("5");
		ck_sex_t.setTag("6");
		ck_sex_p.setTag("7");
		ck_sex_h.setTag("8");
		ck_sex_tc.setTag("9");
		ck_sex_bi.setTag("10");

		boxs[0] = ck_sex_male;
		boxs[1] = ck_sex_female;
		boxs[2] = ck_sex_1;
		boxs[3] = ck_sex_0;
		boxs[4] = ck_sex_05;
		boxs[5] = ck_sex_t;
		boxs[6] = ck_sex_p;
		boxs[7] = ck_sex_h;
		boxs[8] = ck_sex_tc;
		boxs[9] = ck_sex_bi;

		ck_sex_0.setOnClickListener(this);
		ck_sex_05.setOnClickListener(this);
		ck_sex_1.setOnClickListener(this);
		ck_sex_female.setOnClickListener(this);
		ck_sex_h.setOnClickListener(this);
		ck_sex_male.setOnClickListener(this);
		ck_sex_p.setOnClickListener(this);
		ck_sex_t.setOnClickListener(this);
		ck_sex_bi.setOnClickListener(this);
		ck_sex_tc.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		// TODO 自动生成的方法存根
		ck_sex_0.setChecked(false);
		ck_sex_05.setChecked(false);
		ck_sex_1.setChecked(false);
		ck_sex_female.setChecked(false);
		ck_sex_h.setChecked(false);
		ck_sex_male.setChecked(false);
		ck_sex_p.setChecked(false);
		ck_sex_bi.setChecked(false);
		ck_sex_t.setChecked(false);
		ck_sex_tc.setChecked(false);

		((CheckBox) v).setChecked(true);

	}

	private String getCheckedSex() {

		for (CheckBox cBox : boxs) {

			if (cBox.isChecked()) {

				return (String) cBox.getTag();
			}
		}
		return "0";
		// TODO 自动生成的方法存根
	}

	@Override
	public synchronized void requestFinish(JsonObject jsonObjectReturn) {

		watingDialog.cancel();
		JsonObject result = jsonObjectReturn;
		if (result != null) {

			Toast.makeText(getBaseContext(), result.get("desc").getAsString(),
					Toast.LENGTH_LONG).show();

			switch (result.get("state").getAsInt()) {

			case 200:

				Editor editor = sp.edit();
				editor.putString("USER_NAME", str_uname);
				editor.putString("PASSWORD", str_pwd);

				editor.commit();

				Intent intent = new Intent();
				intent.setClass(this, LoginActivity.class);
				startActivity(intent);
				break;
			case 201:

				break;
			}
		}
	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根
		Toast.makeText(getBaseContext(), "网络连接失败，请检查您的网络设置。", Toast.LENGTH_LONG)
				.show();
	}

}
